@extends('layouts.admin.application')

@section('content')
    <div class="main-content">
        <div class="col-xs-12">
            <div class="breadcrumbs" id="breadcrumbs">
                <ul class="breadcrumb">
                    <li><i class="icon-home home-icon"></i><a href="#">首页</a></li>
                    <li>商品分类</li>
                </ul>
            </div>
            <div class="table-responsive">
                <div id="sample-table-2_wrapper" class="dataTables_wrapper" role="grid">
                    <div class="row">

                        @include('layouts.partials._flash')

                        <div class="col-sm-2">
                            <div id="sample-table-2_length" class="dataTables_length">
                                <a href="{{ route('shop.tag.create') }}" class="btn btn-sm btn-primary">新增</a>
                                <a href="javascript:void(0);" class="btn btn-sm btn-purple" id="show_all">展开所有</a>
                            </div>
                        </div>
                    </div>
                    <table id="sample-table-2"
                           class="table table-striped table-bordered table-hover dataTable del-table"
                           aria-describedby="sample-table-2_info">
                        <thead>
                        <tr role="row">
                            <th>编号</th>
                            <th>分类名称</th>
                            <th>缩略图</th>
                            <th>分类商品</th>
                            <th>排序</th>
                            <th>是否显示</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        @foreach($tags as $tag)
                            <tr class="xParent" data-id="{{$tag->id}}" id="row_{{$tag->id}}">
                                <td>{{$tag->id}}</td>
                                <td>
                                    {{$tag->name}}
                                </td>
                                <td></td>
                                <td></td>
                                <td>
                                    <input type="text" class="ace sort_order" value="{{$tag->sort_order}}"
                                           style="width: 50px;">
                                </td>
                                <td>
                                    {!! is_something($tag,'is_show') !!}
                                </td>
                                <td>
                                    <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">
                                        <a class="green" href="{{ route('shop.tag.edit', $tag->id) }}"><i
                                                    class="icon-pencil bigger-130"></i></a>
                                        <a class="red" href="{{route('shop.tag.destroy', $tag->id)}}"
                                           data-method="delete" data-token="{{csrf_token()}}"
                                           data-confirm="确定要删除此条记录么?">
                                            <i class="icon-trash bigger-130"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>

                            @foreach($tag->children as $t)
                                <tr class="xChildren child_row_{{$tag->id}}" data-id="{{$t->id}}"
                                    style="display: none;">
                                    <td>{{$t->id}}</td>
                                    <td>
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|--{{$t->name}}
                                    </td>
                                    <td>
                                        <img src="{{$t->image}}" alt="" style="width: 80px;height: 50px;">
                                    </td>
                                    <td>
                                        {!! show_tag_goods($t) !!}
                                    </td>

                                    <td>
                                        <input type="text" class="ace sort_order" value="{{$t->sort_order}}"
                                               style="width: 50px;">
                                    </td>
                                    <td>
                                        {!! is_something($t,'is_show') !!}
                                    </td>
                                    <td>
                                        <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">
                                            <a class="green" href="{{ route('shop.tag.edit', $t->id) }}"><i
                                                        class="icon-pencil bigger-130"></i></a>
                                            <a class="red" href="{{route('shop.tag.destroy', $t->id)}}"
                                               data-method="delete" data-token="{{csrf_token()}}"
                                               data-confirm="确定要删除此条记录么?">
                                                <i class="icon-trash bigger-130"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('js')
    <script>
        $(function () {
            //展开与折叠表格
            $("tr.xParent").dblclick(function () {
                $(this).toggleClass();
                $(".child_" + this.id).toggle();
            });

            $("#show_all").click(function () {
                $("tr.xParent").toggleClass();
                $("tr.xChildren").toggle('am-active');
            });

            //ajax排序
            $('.sort_order').change(function () {
                var data = {
                    id: $(this).parents('tr').data('id'),
                    sort_order: $(this).val()
                };

                $.ajax({
                    type: 'PATCH',
                    url: '{{ route('shop.tag.sort_order') }}',
                    data: data,
                    success: function (data) {
                        window.location.reload();
                    }
                });

                return false;
            });

            //改变属性
            $('.change_attr').click(function () {
                var _this = $(this);
                var data = {
                    id: $(this).parents('tr').data('id'),
                    attr: $(this).attr('data-attr')
                };

                $.ajax({
                    type: 'PATCH',
                    url: '{{ route('shop.tag.change_attr') }}',
                    data: data,
                    success: function () {
                        _this.toggleClass('icon-ok icon-ban-circle');
                    }
                });

                return false;
            });

            //全选，反选
            $("#check_all").click(function () {
                $(':checkbox').prop("checked", this.checked);
            });

        })
    </script>
@endsection